package cn.vendpay.com.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author eleven
 * 使用Seata对数据源进行代理
 */
@Configuration
public class DataSourceProxyConfig {

    @Bean
    @Primary  // 默认业务数据源
    public DataSource businessDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://192.168.88.138:3306/vend-pay?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("1234");
        // 给 Seata 代理
        return new DataSourceProxy(druidDataSource);
    }

//    // Seata 自己的数据库可以直接用普通数据源（默认SpringBoot配置即可）
//    @Bean
//    public DataSource seataDataSource() {
//        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        druidDataSource.setUrl("jdbc:mysql://192.168.88.138:3306/seata?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true");
//        druidDataSource.setUsername("root");
//        druidDataSource.setPassword("1234");
//        return druidDataSource;
//    }

}